﻿Imports System
Imports System.CodeDom
Imports System.Collections
Imports System.Collections.Generic
Imports System.Text

Namespace System.Workflow.Activities.Rules
    Friend Class NamespaceSymbol
        Inherits Symbol
        ' Methods
        Friend Sub New()
        End Sub

        Friend Sub New(ByVal name As String, ByVal parent As NamespaceSymbol)
            Me.name = name
            Me.Parent = parent
            Me.Level = If((parent Is Nothing), 0, (parent.Level + 1))
        End Sub

        Friend Function AddNamespace(ByVal nsName As String) As NamespaceSymbol
            If (Me.NestedSymbols Is Nothing) Then
                Me.NestedSymbols = New Dictionary([Of] String, Symbol)
            End If
            Dim symbol As Symbol = Nothing
            If Not Me.NestedSymbols.TryGetValue(nsName, symbol) Then
                symbol = New NamespaceSymbol(nsName, Me)
                Me.NestedSymbols.Add(nsName, symbol)
            End If
            Return [TryCast](symbol,NamespaceSymbol)
        End Function

        Friend Sub AddType(ByVal type As Type)
            Dim symbol As New TypeSymbol(type)
            Dim name As String = symbol.Name
            If (Me.NestedSymbols Is Nothing) Then
                Me.NestedSymbols = New Dictionary([Of] String, Symbol)
            End If
            Dim symbol2 As Symbol = Nothing
            If Me.NestedSymbols.TryGetValue(name, symbol2) Then
                Dim symbol3 As OverloadedTypeSymbol = [TryCast](symbol2,OverloadedTypeSymbol)
                If (symbol3 Is Nothing) Then
                    Dim symbol4 As TypeSymbol = [TryCast](symbol2,TypeSymbol)
                    symbol3 = New OverloadedTypeSymbol(name, symbol, symbol4)
                    Me.NestedSymbols.Item(name) = symbol3
                Else
                    symbol3.AddLocalType(symbol)
                End If
            Else
                Me.NestedSymbols.Add(name, symbol)
            End If
        End Sub

        Friend Function FindMember(ByVal memberName As String) As Symbol
            Dim symbol As Symbol = Nothing
            Me.NestedSymbols.TryGetValue(memberName, symbol)
            Return symbol
        End Function

        Friend Function GetMembers() As ArrayList
            Dim list As New ArrayList(Me.NestedSymbols.Count)
            Dim symbol As Symbol
            For Each symbol In Me.NestedSymbols.Values
                symbol.RecordSymbol(list)
            Next
            Return list
        End Function

        Friend Function GetQualifiedName() As String
            Dim builder As New StringBuilder
            Dim stack As New Stack([Of] String)
            stack.Push(Me.Name)
            Dim symbol As NamespaceSymbol = Me.Parent
            Do While (Not symbol Is Nothing)
                stack.Push(symbol.Name)
                symbol = symbol.Parent
            Loop
            builder.Append(stack.Pop)
            Do While (stack.Count > 0)
                builder.Append("."c)
                builder.Append(stack.Pop)
            Loop
            Return builder.ToString
        End Function

        Friend Overrides Function ParseRootIdentifier(ByVal parser As Parser, ByVal parserContext As ParserContext, ByVal assignIsEquality As Boolean) As CodeExpression
            Return parser.ParseRootNamespaceIdentifier(parserContext, Me, assignIsEquality)
        End Function

        Friend Overrides Sub RecordSymbol(ByVal list As ArrayList)
            list.Add(Me.Name)
        End Sub


        ' Properties
        Friend Overrides ReadOnly Property Name As String
            Get
                Return Me.name
            End Get
        End Property


        ' Fields
        Friend ReadOnly Level As Integer
        Private name As String
        Friend NestedSymbols As Dictionary([Of] String, Symbol)
        Friend ReadOnly Parent As NamespaceSymbol
    End Class
End Namespace

